import axios from "axios";
import store from "@/store";
import router from "@/router";
import { message } from "@/components/Message";

// baseURL设置/超时时间设置
const instance = axios.create({
  // baseURL: "https://apipc-xiaotuxian-front.itheima.net",
   baseURL: "http://pcapi-xiaotuxian-front-devtest.itheima.net",
  timeout: 5000,
});

// 添加请求拦截器
instance.interceptors.request.use(
  function (config) {
    // 1. 获取token
    const { token } = store.state.user.profile;
    // 2. 请求头设置token
    if (token) {
      config.headers.Authorization = `Bearer ${token}`;
    }
    return config;
  },
  function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
  }
);

// 添加响应拦截器
instance.interceptors.response.use(
  function (response) {
    //console.log(response, 8);
    return response.data;
  },
  function (error) {
    console.dir(error.response, 9);
    //  用户不存在或者密码不对的
    // if (error.response.data.code == 17001) {
    //   return message({
    //     type: "error",
    //     text: error.response.data.message,
    //   });
    // }
    if (error.response && error.response.status === 400) {
      return message({
        type: "error",
        text: error.response.data.message,
      });
    }
    // 对响应错误做点什么
    if (error.response && error.response.status === 401) {
      const redirectUrl = encodeURIComponent(
        router.currentRoute.value.fullPath
      );
      router.push("/login?redirectUrl=" + redirectUrl);
    }
    return Promise.reject(error);
  }
);

/**
 * @param {String} - url  请求地址
 * @param {String} - method  请求类型
 * @param {Object} - submitData  对象类型，提交数据
 */
const request = (url, method, submitData) => {
  return instance({
    url,
    method,
    [method.toLowerCase() === "get" ? "params" : "data"]: submitData,
  });
};

export default request;
